<template>
  <div class="catemanager">
    <myheader title="频道管理">
      <span slot="left" class="iconfont iconjiantou2" @click="$router.back()"></span>
    </myheader>
    <div class="mychannel">
      <h3>点击删除频道</h3>
      <div class="list">
        <span  v-for="(v,i) in cateList" :key='i' @click="remove(v,i)">{{v.name}}</span>
      </div>
    </div>
    <div class="youchannel">
      <h3>点击添加频道</h3>
      <div class="list">
        <span  v-for="(v,i) in uncateList" :key='i' @click="add(v,i)">{{v.name}}</span>
      </div>
    </div>
  </div>
</template>

<script>
import myheader from "@/components/myheader.vue";
import {getCateList} from '@/apis/category.js'
export default {
  data() {
    return {
      cateList:[],     // 已添加的栏目数据  hm_toutiao_has_cate
      uncateList:[]   // 未添加的栏目数据 hm_toutiao_nothas_cate
    };
  },
  components: {
    myheader,
  },
  methods: {
    remove(v,i){
      // 将当前栏目添加到已删除的栏目数据中
      this.uncateList.push(v)
      // 将当前栏目从已添加栏目中移除
      this.cateList.splice(i,1)
      // 实现本地存储
      localStorage.setItem('hm_toutiao_has_cate',JSON.stringify(this.cateList))
      localStorage.setItem('hm_toutiao_nothas_cate',JSON.stringify(this.uncateList))
    },
    add(v,i){
      this.cateList.push(v)
      this.uncateList.splice(i,1)
      // 实现本地存储
      localStorage.setItem('hm_toutiao_has_cate',JSON.stringify(this.cateList))
      localStorage.setItem('hm_toutiao_nothas_cate',JSON.stringify(this.uncateList))
    }
  },
  async mounted () {
    // 获取所有栏目的数据，先判断是否有本地存储，如果没有则获取服务器数据，如果有，则直接读取本地存储
    if(localStorage.getItem('hm_toutiao_has_cate')){
      this.cateList = JSON.parse(localStorage.getItem('hm_toutiao_has_cate'))
    }else{
      this.cateList = (await getCateList()).data.data
      console.log(this.cateList);
      if (localStorage.getItem("hm_toutiao")) {
          this.cateList = this.cateList.splice(2);// 干掉关注和头条
      } else {
          this.cateList = this.cateList.splice(1); // 干掉头条
      }
    }
    if(localStorage.getItem('hm_toutiao_nothas_cate')){
      this.uncateList = JSON.parse(localStorage.getItem('hm_toutiao_nothas_cate'))
    }
    console.log(this.cateList);
  }
};
</script>

<style lang="less" scoped>
.mychannel,
.youchannel {
  padding: 10px;
  h3 {
    line-height: 30px;
    font-weight: bold;
  }
  .list {
    padding: 10px;
    display: flex;
    flex-wrap: wrap;
    > span {
      width: 20%;
      margin-left: 10px;
      margin-top: 10px;
      border: 1px solid #ccc;
      text-align: center;
      line-height: 40px;
    }
  }
}
</style>